'use strict'

class adminlist {

    constructor() {
        //param默认就是/:id，若要改变路由，则可以设置此属性，比如设置为/:name
        this.param = '/'
    }

    // 添加管理员用户
    async post(c) {
        var obj = c.body
        if (obj) {
            var { manage_account, manage_pwd, manage_avatar } = obj
            let row = await c.service.psql.query(`select * from manage order by manage_id asc`)
            let manage_id = parseInt(row.rows[row.rows.length - 1].manage_id) + 1
            console.log(manage_id)
            console.log(manage_account, manage_pwd, manage_avatar)
            let ret = await c.service.psql.query(`insert into manage values(${manage_id}, '${manage_account}', '${manage_pwd}', '${manage_avatar}')`)
            console.log(ret)
            c.status(200).send('已接收')
        } else {
            c.status(400).send('please check your data :)')
        }

    }

    // 获取管理员用户列表 增序
    async get(c) {
        let ret = await c.service.psql.query(`select * from manage order by manage_id asc`)
        c.status(200).send(ret.rows);
    }

    // 删除管理员用户
    async delete(c) {
        if (!c.body.isRows) {
            var id = c.body.id
            console.log(id)
            if (id) {
                let ret = await c.service.psql.query(`delete from manage where manage_id = ${parseInt(id)}`)
                console.log(ret)
                c.status(200).send('delete success')

            } else {
                c.status(400).send('please check id')
            }
        } else {
            var idArr = c.body.idArr
            if (idArr) {
                for (let i = 0; i < idArr.length; i++) {
                    let ret = await c.service.psql.query(`delete from manage where manage_id = ${parseInt(idArr[i])}`)
                    console.log(ret)
                }
                c.status(200).send('delete complete')
            } else {
                c.status(400).send('failure')
            }
        }
    }

    // 更新管理员用户信息
    async put(c) {
        var req = c.body
        console.log(req)
        if (req.manage_account) {
            let ret = await c.service.psql.query(`update manage set manage_account = '${req.manage_account}' where manage_id = ${parseInt(req.manage_id)}`)
            console.log('account', ret)
        }
        if (req.manage_pwd) {
            let ret = await c.service.psql.query(`update manage set manage_pwd = '${req.manage_pwd}' where manage_id = ${parseInt(req.manage_id)}`)
            console.log('pwd', ret)
        }
        if (req.manage_avatar) {
            let ret = await c.service.psql.query(`update manage set manage_avatar = '${req.manage_avatar}' where manage_id = ${parseInt(req.manage_id)}`)
            console.log('avatar', ret)
        }
        c.status(200).send('complete')
    }
}

module.exports = adminlist
